<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="description" content="nodejsscan is a static security code scanner for Node.js applications.">
      <meta name="author" content="Ajin Abraham">
      <link rel="apple-touch-icon" sizes="76x76" href="{{ url_for('static', filename='assets/img/apple-touch-icon.png') }}">
      <link rel="icon" type="image/png" href="{{ url_for('static', filename='assets/img/favicon.ico') }}">
      <title>nodejsscan v{{version}} </title>
      <link href="{{ url_for('static', filename='assets/css/index.css') }}" rel="stylesheet">
      <link href="https://fonts.googleapis.com/css?family=Montserrat:400,700" rel="stylesheet" type="text/css">
   </head>
   <body>
    
      <div class="site-wrapper">
         <div class="site-wrapper-inner">
            <div class="cover-container">
               <div class="masthead clearfix">
                  <div align="center" class="inner">
                      <div class="row">
                        <div class="col-md-8 col-md-offset-2">
                            <img src ="{{ url_for('static', filename='assets/img/nodejsscan.png') }}" width="25%" height="35%"><h1 class="brand-heading">nodejsscan</h1>
                            
                             <p class="intro-text">
                                <input id="repo" type="text" placeholder="git" class="repourl" />
                                <button id="git" class="clone">Clone</button></p>
                           
                        </div>
                    </div>
                  </div>
               </div>
               <div class="inner cover">
                  <div style="visibility:hidden; opacity:0" id="drop">
                     <div id="textnode">Drop anywhere!</div>
                  </div>
                  </br>
                   
                  <form id="upload_form" enctype="multipart/form-data" method="post">
                     <div class="fileUpload btn btn-default btn-lg" id="but">
                        <input type="file" name="file" id="uploadFile" placeholder="Choose File" multiple>
                        <span class="fas fa-cloud-upload-alt"></span>
                        UPLOAD &amp; SCAN
                     </div>
                     <br />
                     <small><i>Drag &amp; Drop anywhere!</i></small>
                     <h5 id="status"></h5>
                     <progress id="progressBar" value="0" max="100" style="width:300px;visibility:hidden;" ></progress>
                  </form>
               </div>
               <div class="mastfoot">
                  <div class="inner">
                     </br>
                     <div class="copyright">
                      <h4><a href="/scans/"> RECENT SCANS</a></h4>
                      &copy; {{ year }}  <strong>nodejsscan v{{ version }}</strong> | <a href="https://ajinabraham.com">Ajin Abraham</a> | <a href="https://opensecurity.in">OpenSecurity</a>
                    </div>
                  </div>
               </div>
            </div>
         </div>
      </div>
      <script src="{{ url_for('static', filename='assets/js/core/jquery.min.js') }}"></script>
      <script>
            function _(el){
                return document.getElementById(el);
            }
            //analyzing
            function analyzing(){
                    var i = 1;
                    return setInterval(function() {
                        var dot = '';
                        for (var x = 1; x <= i; x++) {
                            dot += '.';
                        }
                        _("status").innerText = "Analyzing" + dot;
                        i += 1;
                        if (i == 5) {
                            i = 1;
                        }
                    }, 2000);
            }
            // Response Handler     
            function responseHandler(json, isbutton) {
                if (json.status === 'failed') {
                    _("status").innerText = json.message;
                } else if (json.status === 'success') {
                    window.location.href = window.location.href + json.url;
                }
            }
            function progressHandler(event) {
                var percent = (event.loaded / event.total) * 100;
                console.log("Uploaded..." + Math.round(percent));
                _("progressBar").value = Math.round(percent);
                _("status").innerText = Math.round(percent) + "% Uploaded...";
                if (percent === 100){
                    analyzing();
                }
            }
            function completeHandler(event) {
                var json = JSON.parse(event.target.responseText);
                responseHandler(json);
            }
            function errorHandler(event) {
                _("status").innerText = "Upload Failed!";
            }
            function abortHandler(event) {
                _("status").innerText = "Upload Aborted!";
            }
          // Is valid file extentions
          function isValidExt(file_name){
                var val = file_name.toLowerCase();
                var regex = new RegExp("(.*?)\.(zip)$");
                val = val.replace(/^.*[\\\/]/, '');
                if (!(regex.test(val))) {
                    _('status').innerText = "Please upload ZIP only!";
                    return false;
                }
                return true;
          }
          // Is allowed mimetype
          function isValidMime(file_mime){
                if (file_mime.length < 1)
                    // bypass if mime is not available
                    return true; 
                var supported = [{% for mime in mimes %}'{{mime}}',{% endfor %}];
                if(supported.indexOf(file_mime) >-1)
                    return true;
                 _('status').innerText = "MIME type (" + file_mime + ") is not supported!";
                return false;
          }
         // File Upload
         function uploadFile(file, i) {
           try {
                if (!isValidExt(file.name) || !isValidMime(file.type)){
                    return;
                }
                _("progressBar").style.visibility = "visible";
                var url = '/upload/'
                var xhr = new XMLHttpRequest()
                xhr.open('POST', url, true)
                xhr.upload.addEventListener("progress", progressHandler, false);
                xhr.addEventListener("load", completeHandler, false);
                xhr.addEventListener("error", errorHandler, false);
                xhr.addEventListener("abort", abortHandler, false);
                var formdata = new FormData();
                formdata.append("file", file);
                xhr.send(formdata)
            } catch (e) {
                alert("Error:" + e);
            }
           }
         
           function handleFiles(files) {
               files = [...files]
               files.forEach(uploadFile)
           }
         
           // Full Screen Drag & Drop File Upload
           var lastTarget = null;
           function isFile(evt) {
               var dt = evt.dataTransfer;
         
               for (var i = 0; i < dt.types.length; i++) {
                   if (dt.types[i] === "Files") {
                       return true;
                   }
               }
               return false;
           }
         
           window.addEventListener("dragenter", function (e) {
               if (isFile(e)) {
                   lastTarget = e.target;
                   document.querySelector("#drop").style.visibility = "";
                   document.querySelector("#drop").style.opacity = 1;
                   document.querySelector("#textnode").style.fontSize = "48px";
               }
           });
         
           window.addEventListener("dragleave", function (e) {
               e.preventDefault();
               if (e.target === document || e.target === lastTarget) {
                   document.querySelector("#drop").style.visibility = "hidden";
                   document.querySelector("#drop").style.opacity = 0;
                   document.querySelector("#textnode").style.fontSize = "42px";
               }
           });
         
           window.addEventListener("dragover", function (e) {
               e.preventDefault();
           });
         
           window.addEventListener("drop", function (e) {
               e.preventDefault();
               document.querySelector("#drop").style.visibility = "hidden";
               document.querySelector("#drop").style.opacity = 0;
               document.querySelector("#textnode").style.fontSize = "42px";
               if(e.dataTransfer.files.length > 0)
               {
               handleFiles(e.dataTransfer.files);
               }
           });

            $(document).ready(function() {
                // Button File Upload
                $('input[type=file]').change(function() {
                    _('status').innerText = "";
                    if (_("uploadFile").files.length === 0) {
                        return;
                    }
                    _("uploadFile").style.display = "none";
                    var files = _("uploadFile").files;
                    handleFiles(files);
                });
                //Button Clone
                var analyze;
                 $('#git').on('click', function () {
                   $.post('/git/', {
                        url: $('#repo').val()
                    },
                    function (result) {
                        if (result.status === "success") {
                             window.location.href = window.location.href + result.url;
                        } else {
                            clearInterval(analyze);
                            _("status").innerText = result.message;
                        }
                    });
                    analyze = analyzing();
                });
                //doc ready
            });
            
      </script>
   </body>
</html>